#!/bin/bash

echo "=== AI提示生成系统测试 ==="
echo

# 测试1：直接访问8002端口
echo "1. 测试直接访问8002端口..."
RESPONSE=$(curl -s -X POST http://localhost:8002/api/v1/ai/generate-hints \
  -H "Content-Type: application/json" \
  -d '{"topic": "AI测试", "purpose": "audience"}')

if echo "$RESPONSE" | grep -q "hints"; then
  echo "✅ 直接访问8002端口成功"
else
  echo "❌ 直接访问8002端口失败"
  echo "Response: $RESPONSE"
fi
echo

# 测试2：通过Vite代理访问
echo "2. 测试通过Vite代理访问..."
PROXY_RESPONSE=$(curl -s -X POST http://localhost:13000/api/v1/ai/generate-hints \
  -H "Content-Type: application/json" \
  -d '{"topic": "代理测试", "purpose": "viewpoint"}')

if echo "$PROXY_RESPONSE" | grep -q "hints"; then
  echo "✅ 通过Vite代理访问成功"
  echo "   返回了 $(echo "$PROXY_RESPONSE" | grep -o '"hints":\[[^]]*\]' | grep -o '\[.*\]' | tr ',' '\n' | wc -l) 个提示"
else
  echo "❌ 通过Vite代理访问失败"
  echo "Response: $PROXY_RESPONSE"
fi
echo

# 测试3：批量生成
echo "3. 测试批量生成..."
BATCH_RESPONSE=$(curl -s -X POST http://localhost:13000/api/v1/ai/generate-hints/batch \
  -H "Content-Type: application/json" \
  -d '{"topic": "批量测试", "purposes": ["audience", "viewpoint", "material"]}')

if echo "$BATCH_RESPONSE" | grep -q "audience"; then
  echo "✅ 批量生成成功"
else
  echo "❌ 批量生成失败"
  echo "Response: $BATCH_RESPONSE"
fi
echo

# 测试4：检查前端页面
echo "4. 检查前端测试页面..."
if curl -s http://localhost:13000/test/hints-test | grep -q "AI动态提示生成测试"; then
  echo "✅ 前端测试页面可访问"
  echo "   访问地址: http://localhost:13000/test/hints-test"
else
  echo "❌ 前端测试页面不可访问"
fi
echo

echo "=== 测试完成 ==="
echo
echo "📝 总结："
echo "- API端点: http://localhost:8002/api/v1/ai/generate-hints"
echo "- 代理端点: http://localhost:13000/api/v1/ai/generate-hints"
echo "- 测试页面: http://localhost:13000/test/hints-test"
echo "- 支持的purpose类型: audience, viewpoint, material, style, goal"